import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;


@SuppressWarnings("Duplicates")
@WebServlet("jdbc2")
public class Jdbc2Servlet extends HttpServlet {

    private static final String URL = "jdbc:h2:~/homework;MODE=MYSQL;DB_CLOSE_DELAY=-1";
    private static final String NAME = "sa";
    private static final String PASSWORD = "sa";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

        // 每次执行都新建一个连接
        try (Connection conn = DriverManager.getConnection(URL, NAME, PASSWORD)) {

            //3.通过数据库的连接操作数据库，实现增删改查
            Statement stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery("select * from grade");//选择import java.sql.ResultSet;

            // 遍历每行记录
            while (rs.next()) {
                //如果结果集中有数据，就会循环打印出来
                System.out.println(rs.getString("id") + "," + rs.getString("course_name")+
                        rs.getString("score"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
